<template>
  <div class="">
    <el-form :inline="true">
      <div style="display: flex; justify-content: space-between;">
        <div />
        <el-form-item>
          <el-button type="primary" @click="onVisible(1)">新增分类</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center" />
      <el-table-column prop="typename" label="分类名称" align="center" />
      <el-table-column prop="typecode" label="分类编码" align="center" />
      <el-table-column prop="description" label="分类描述" align="center" />
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state==0" type="danger">禁用</el-tag>
          <el-tag v-else type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="onVisible(2, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="totalCount>10"
      layout="prev, pager, next"
      :total="totalCount"
      @current-change="idx=>{PageIndex = idx}"
    />
    <el-dialog
      :title="visibleType=='1'?'新增分类':'修改分类'"
      :visible.sync="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
      @closed="handleClose"
    >
      <div>
        <el-form ref="forms" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="typename">
            <el-input v-model="form.typename" placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item label="分类编码" prop="typecode">
            <el-input v-model="form.typecode" placeholder="请输入分类编码" />
          </el-form-item>
          <el-form-item label="分类描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入分类描述" />
          </el-form-item>
          <el-form-item label="启用/禁用" prop="state">
            <el-switch v-model="form.state" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { mapState } from 'vuex'

import { GetTypes, AddEditType, DeleteType } from '@/api/news.js'
export default {
  name: 'Name',
  data() {
    return {
      dialogImageUrl: '',
      dialogImg: false,
      form: {},
      uploadUrl: process.env.VUE_APP_BASE_API + "/ctrls/Upload?type='img'&file_type=''",
      headers: {
        TOKEN: getToken()
      },

      listLoading: false,
      tableData: [],
      totalCount: 0,
      PageIndex: 1,
      dialogVisible: false,
      visibleType: '',
      rules: {
        title: [
          { required: true, message: '请输入配置说明', trigger: 'blur' }
        ],
        rechargeamount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        inamount: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请选择排序', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    PageIndex(val) {
      console.log(val)
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    setFrom() {
      this.form = {
        idx: 0,
        typename: '',
        typecode: '',
        description: '',
        state: 1
      }
    },
    // 上传成功
    quillImgSuccess({ success, data }, file) {
      this.form.picurl = data
      console.log(this.form.picurl)
    },

    // 删除
    handleRemove({ status, url }, file) {
      this.form.picurl = ''
    },
    // 预览
    handleSuccess(file) {
      this.dialogImageUrl = file
      this.dialogImg = true
    },
    async onDelete(item) {
      try {
        const confirm = await this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm == 'confirm') {
          const { success, msg } = await DeleteType([item.idx])
          this.getData()
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        }
      } catch (e) {
        // 取消
        console.log(e)
      }
    },
    // 状态
    async SetValid(val, item, type) {
      const { success, msg } = await RechargediscountSetState({
        state: val
      }, [item.id])
      if (success) {
        this.getData()
      }
      this.$message({
        message: msg,
        type: success ? 'success' : 'error'
      })
    },
    onEdit() {
      this.$refs['forms'].validate(async(valid) => {
        if (valid) {
          const { success, msg } = await AddEditType(this.form)
          if (success) {
            this.dialogVisible = false
            this.getData()
          }
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        } else {
          return false
        }
      })
    },
    // 关闭前
    handleClose() {
      this.$refs.forms.resetFields()
    },
    // 打开前
    onVisible(type, item = {}) {
      this.visibleType = type
      this.setFrom()
      this.form = { ...this.form, ...item }
      this.dialogVisible = true
    },
    async onSearch() {},
    async getData(address) {
      this.listLoading = true
      const { list, pageIndex, totalCount } = await GetTypes({
        PageIndex: this.PageIndex,
        PageSize: 10
      })
      this.totalCount = totalCount
      console.log(list)
      this.tableData = list
      this.listLoading = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
<style lang="scss">
  .el-table .warning-row {
    background: oldlace;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 100%;
    display: block;
  }
</style>
